package com.coda.controller.user;

import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.vo.UserWordVo;
import com.coda.service.UserWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 用户单词controller
 */
@RestController
@RequestMapping("/user/userWord")
public class UserWordController {

    @Autowired
    private UserWordService userWordService;

    /**
     * 删除已经默写过的单词
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = userWordService.delete(id);
        return result > 0 ? Result.success() : Result.error(MessageConstant.DELETE_ERR);
    }

    /**
     * 收藏单词
     */
    @PostMapping("/collect")
    public Result collect(Integer id ) {
        UserWordVo userWordVo = new UserWordVo();
        userWordVo.setWordId(id);
        userWordVo.setUserId(BaseContext.getCurrentId());
        userWordVo.setCreateTime(LocalDateTime.now());
        int result = userWordService.collect(userWordVo);
        return result > 0 ? Result.success() : Result.error(MessageConstant.COLLECT_ERR);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/collect/{id}")
    public Result deleteCollect(@PathVariable Integer id) {
        int result = userWordService.deleteCollect(id);
        return result > 0 ? Result.success() : Result.error(MessageConstant.DELETE_ERR);
    }
}
